package com.uniledger.exchange.bank;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniledger.exchange.R;
import com.uniledger.exchange.glide.GlideApp;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BankListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    List<BankCard> bankList;
    Context context;
    LayoutInflater inflater;

    public BankListAdapter(Context context) {
        this.context = context;
        this.bankList = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
    }

    public void addData(List<BankCard> list) {
        this.bankList.addAll(list);
        notifyDataSetChanged();
    }

    public BankCard getItem(int pos) {
        return bankList.get(pos);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item_select_bank, parent, false);
        return new BankViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        BankViewHolder vh = (BankViewHolder) holder;
        BankCard bank = bankList.get(position);
        vh.bankName.setText(bank.bank);

        GlideApp.with(context).load(bank.bankIcon)
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .circleCrop()
                .into(vh.imageView2);

    }

    @Override
    public int getItemCount() {
        return bankList.size();
    }

    static class BankViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_bank_name)
        TextView bankName;

        @BindView(R.id.imageView2)
        ImageView imageView2;

        public BankViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
